var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = navigator.userAgent.indexOf('Opera') != -1;
var nowWindow = -1;//当前选中窗体id
var maxWindow = 0;
var ocs_tab = new Array();//单击tabmenu事件方法数组
var wl = 100;//每个tabmenu的宽度
var bLeft = false, bRight = false;//左右是否有滚动条
var urls = new Array();
//单击url链接时启动事件
function onClubClick(id, url, target,title)
{
	MDIOpen(url,target,title);
}
function scrollTab(s){
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;		
	var w = tabmenu.document.body.offsetWidth;
	var l = mdiframe.frames.length * wl;
	tabmenu.scrollTo(s, 0);
	if(s){
		document.getElementById("tabmenu_left").src = "http://img.ku6.com/common/tabmenu/tabmenu_left.gif";
		document.getElementById("tabmenu_left").alt="点击向左滚动";
		bLeft = true;
	}else{
		document.getElementById("tabmenu_left").src = "http://img.ku6.com/common/tabmenu/tabmenu_left_gray.gif";
		document.getElementById("tabmenu_left").alt="没有需要滚动的";
		bLeft = false;
	}
	if(s + w < l){
		document.getElementById("tabmenu_right").src = "http://img.ku6.com/common/tabmenu/tabmenu_right.gif";
		document.getElementById("tabmenu_right").alt="点击向右滚动";
		bRight = true;
	}else{
		document.getElementById("tabmenu_right").src = "http://img.ku6.com/common/tabmenu/tabmenu_right_gray.gif";
		document.getElementById("tabmenu_right").alt="没有需要滚动的";
		bRight = false;
	}
}
function onresizeH(e)
{
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;		
	var w = tabmenu.document.body.offsetWidth;
	var l = mdiframe.frames.length * wl;
	var s = tabmenu.scrollX ? tabmenu.scrollX : tabmenu.document.body.scrollLeft;
	if(nowWindow != -1)
	{
		changeParentHeight();
		var p = tabmenu.document.getElementById("tab_" + nowWindow).offsetLeft;
		if(l == wl * 2){
			setTimeout("tabmenu.scrollTo(0, 0);", 0);
			return;
		}else if(l == wl){
			document.getElementById("tabmenu_left").src = "http://img.ku6.com/common/tabmenu/tabmenu_left_gray.gif";
			document.getElementById("tabmenu_left").alt="没有需要滚动的";
			bLeft = false;
			document.getElementById("tabmenu_right").src = "http://img.ku6.com/common/tabmenu/tabmenu_right_gray.gif"
			document.getElementById("tabmenu_right").alt="没有需要滚动的";
			bRight = false;
			return;
		}
		if(p < s){
			s = p;
			if(l < w + s)
				s = l - w;
		}else if(p > w + s - wl){
			s = p - w + wl;
			if(l < w + s)s = l - w;
		}else if(l < w + s){
			s = l - w;
		}else{
			 return;
		}
		if(s < 0)
			s = 0;
		scrollTab(s);
	}

}
function changeParentHeight(){
	if(nowWindow!=-1){
		var h;
		try{
			var documentObj=eval("mdiframe.mdi_" + nowWindow);
			if (documentObj.contentDocument && documentObj.contentDocument.body.offsetHeight){ //如果用户的浏览器是NetScape
				h = documentObj.contentDocument.body.offsetHeight; 
			}else if (documentObj.document && documentObj.document.body.scrollHeight){ //如果用户的浏览器是IE
				h = documentObj.document.body.scrollHeight+20;
			}
			h=h<556?556:h;	
			top.mdiframe.height=h;	
		}catch(e){
			
		}	
	}
}

onresize = onresizeH;
//当点击向左时
function onLefttab()
{
	if(!bLeft){return;}
	tabmenu=top.tabmenu;
	var s = tabmenu.scrollX ? tabmenu.scrollX : tabmenu.document.body.scrollLeft;
	s -= s % wl;
	if(s > 0)
		s -= wl;
	var i=nowWindow-1;
	while(0<i){
		if(eval("mdiframe.mdi_" + i)){
			MDISwitch(i);
			break;
		}
		i--;
	}
	scrollTab(s);
}
//当点击向左时
function onRighttab()
{
	if(!bRight){return;}
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;		
	var w = tabmenu.document.body.offsetWidth;
	var l = mdiframe.frames.length * wl;
	var s = tabmenu.scrollX ? tabmenu.scrollX : tabmenu.document.body.scrollLeft;
	s += wl;
	if(s + w > l)
		s = l - w;
	var i=nowWindow+1;
	while(i<maxWindow){
		if(eval("mdiframe.mdi_" + i)){
			MDISwitch(i);
			break;
		}
		i++;
	}	
	scrollTab(s);
}
//双击tabmenu时关闭窗体
function onTabDblClock()
{
	MDIClose();
}
function MDIClose()
{
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;		
	var i, f, la = 0, lw;
	if(nowWindow != -1)
	{
		tabmenu.document.getElementById("tab_list").removeChild(tabmenu.document.getElementById("tab_" + nowWindow));
		mdiframe.document.body.removeChild(mdiframe.document.getElementById("mdi_" + nowWindow));
		
		for(i = 0; i < maxWindow; i ++)
		{
			if(nowWindow != i)
			{
				f = mdiframe.document.getElementById("mdi_" + i)
				if(f)
				{
					if(la < f.LastAccess)
					{
						la = f.LastAccess;
						lw = i;
					}
				}
			}
		}

		nowWindow = -1;
		if(la)MDISwitch(lw);
	}

	return false;
}
//当单击tabmenu时调用选择事件
function MDISwitch(i)
{
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;	
	if(nowWindow == i)return false;
	var ow, nw;
	if(nowWindow != -1)
	{
		tabmenu.document.getElementById("tab_" + nowWindow).className = "tab-unselect";
		ow = mdiframe.document.getElementById("mdi_" + nowWindow);
		ow.scrollPos = 0;
		if(ow.Attached && ow.contentWindow)if(ow.contentWindow.document)if(ow.contentWindow.document.body)
			ow.scrollPos = ow.contentWindow.document.body.scrollTop;
		ow.LastAccess = (new Date()).getTime();
		ow.style.display = "none";
	}
	nw = mdiframe.document.getElementById("mdi_" + i);
	nw.style.display = "";
	try
	{
		if(nw.scrollPos)
			nw.contentWindow.scrollTo(0, nw.scrollPos);
	}catch(e){};

	nowWindow = i;
	tabmenu.document.getElementById("tab_" + nowWindow).className = "tab-select";
	onresizeH();
	return false;
}
//当点击刷新按钮调用事件
function MDIRefresh()
{
	var f;
	var l;
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;	
	f = mdiframe.document.getElementById("mdi_" + nowWindow);
	l = "";
	try
	{
		if(f)
			if(f.contentDocument)
				f.contentDocument.location = f.contentDocument.location;
			else
				eval("mdiframe.mdi_" + nowWindow).document.location = eval("mdiframe.mdi_" + nowWindow).document.location;
	}catch(e){}
}
//打开一个新的窗体
function MDIOpen(url, target,title)
{
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;	
	var i, f, l, l1, n;
	var url1 = url.toLowerCase();
	for(i = 0; i < maxWindow; i++)
	{
		if(target=='_blank')
			break;
		f = mdiframe.document.getElementById("mdi_" + i);
		if(!f){
			continue;
		}
		if(f.name.toLowerCase()==target.toLowerCase()){
			MDISwitch(i);
			if(f.contentDocument){
				if(confirm(f.contentDocument.title+"窗口已打开，你确定要重新载入吗？")){
					f.contentDocument.location=url; 
					MDISwitch(i);
				}
			}else{
				if(confirm(eval("mdiframe.mdi_" + nowWindow).document.title+"窗口已打开，你确定要重新载入吗？")){
					f.src= url; 
					MDISwitch(i);
				}
			}
			return;
		}
	}

	if(maxWindow > mdiframe.frames.length){
		for(i = 0; i < maxWindow; i ++)
			if(!mdiframe.document.getElementById("mdi_" + i))
				break;
	}else{
		i = maxWindow;
		maxWindow ++;
		ocs_tab[i] = new Function("MDISwitch(" + i + ");");
	}

	var nw = mdiframe.document.createElement("iframe")
	nw.width = "100%"
	nw.height = "100%"
	nw.frameBorder = 0;
	nw.scrolling = "yes"
	nw.id = "mdi_" + i;
	nw.name = target;	
	nw.LastAccess = (new Date()).getTime();
	nw.Attached = false;
	nw.src = "about:blank";	
	nw.style.display = "none";
	mdiframe.document.body.appendChild(nw);
	var td = tabmenu.document.createElement("td");
	td.className = "tab-unselect";
	td.width = wl;
	td.id = "tab_" + i;
	td.innerHTML = "　　加载中…";	
	td.onclick=ocs_tab[i];
	if(title){
		var close_pic='';
		if(nowWindow!=0){
			close_pic="<span onclick='parent.onTabDblClock()' class='tabmenu_close'></span>";
		}
		td.innerHTML = "<span class='tabmenu_title'>　" + HtmlEncode(title) + "</span>"+close_pic;
	}
	if(i>0){
		td.ondblclick=onTabDblClock;
	}
	tabmenu.document.getElementById("tab_list").appendChild(td);
	MDISwitch(i);
	MDILoad(i,url);
}
//加载窗体事件
function MDILoad(n, url)
{
	var f;
	var l;
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;		
	f = mdiframe.document.getElementById("mdi_" + n);
	l = "";
	try
	{
		if(f)
			if(f.contentDocument){
				l = f.contentDocument.location = url;
			}	
			else{
				l = eval("mdiframe.mdi_" + n).document.location = url;
			}
	}catch(e){
		
	}
}

function HtmlEncode(s)
{
	var s1;

	s1 = s.replace(new RegExp('&', 'g'), '&amp;');
	s1 = s1.replace(new RegExp('<', 'g'), '&lt;');
	s1 = s1.replace(new RegExp('>', 'g'), '&gt;');

	return s1;
}
function aa(title){
	tabmenu=top.tabmenu;
	mdiframe=top.mdiframe;
	if(typeof title != 'undefined')
	{
		var s = new String(title);
		var l, n;
		l = 0;
		n = 0;
		while(l < 8 && n < s.length)
		{
			if(s.charCodeAt(n) > 128)
			{
				l ++;
				if(l == 10)break;
			} 
			l ++;
			n ++;
		}
		if(n > 0)
		{
			changeParentHeight();
			var close_pic='';
			if(nowWindow!=0){
				close_pic="<span onclick='parent.onTabDblClock()' class='tabmenu_close'></span>";
			}
			if(isOpera)
				tabmenu.document.getElementById("tab_" + nowWindow).innerHTML = "<span class='tabmenu_title'>　" + HtmlEncode(s.substr(0, n)) + "</span>"+close_pic;
			else
				tabmenu.document.getElementById("tab_" + nowWindow).innerHTML = "<span class='tabmenu_title'>" + HtmlEncode(s.substr(0, n)) + "</span>"+close_pic;
			mdiframe.document.getElementById("mdi_" + nowWindow).Attached = true;
		}
	}
}